package cn.jhc.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.UserCookie;
import cn.jhc.bean.User;

/**
 * Servlet implementation class ClearCartServlet
 */
@WebServlet("/user/clearcart.do")
public class ClearCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClearCartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 * 
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie[] cookies = request.getCookies();
		User user = (User)request.getSession().getAttribute("currentUser");
		String username = user.getLoginname();
		Cookie c = UserCookie.tranversal(cookies, username);
		if(c != null && !(c.getValue().equals("")))
		{
			c.setValue("");
			response.addCookie(c);
		}
		response.sendRedirect("./showcart.do");
	}

}
